/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.product.service.impl;

import co.yixiang.modules.product.domain.YxTestQuestionRecord;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.product.service.YxTestQuestionRecordService;
import co.yixiang.modules.product.service.dto.YxTestQuestionRecordDto;
import co.yixiang.modules.product.service.dto.YxTestQuestionRecordQueryCriteria;
import co.yixiang.modules.product.service.mapper.YxTestQuestionRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2022-05-09
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxTestQuestionRecord")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxTestQuestionRecordServiceImpl extends BaseServiceImpl<YxTestQuestionRecordMapper, YxTestQuestionRecord> implements YxTestQuestionRecordService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxTestQuestionRecordDto> queryAll(YxTestQuestionRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxTestQuestionRecord> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxTestQuestionRecordDto.class);
    }


    @Override
    //@Cacheable
    public List<YxTestQuestionRecord> queryAll(YxTestQuestionRecordQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxTestQuestionRecord.class, criteria));
    }


    @Override
    public void download(List<YxTestQuestionRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxTestQuestionRecordDto yxTestQuestionRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户手机号", yxTestQuestionRecord.getUserPhone());
            map.put(" uid",  yxTestQuestionRecord.getUid());
            map.put("创建日期", yxTestQuestionRecord.getCreateTime());
            map.put(" updateTime",  yxTestQuestionRecord.getUpdateTime());
            map.put(" isDel",  yxTestQuestionRecord.getIsDel());
            map.put(" level",  yxTestQuestionRecord.getLevel());
            map.put("昵称", yxTestQuestionRecord.getNickname());
            map.put("微信头像地址", yxTestQuestionRecord.getHeadimgurl());
            map.put("考试通过分数", yxTestQuestionRecord.getAdoptGrade());
            map.put("考试实际分数", yxTestQuestionRecord.getTotalGrade());
            map.put("考试通过标识(0-未通过;1-通过)", yxTestQuestionRecord.getIsAdopt());
            map.put("答题集合", yxTestQuestionRecord.getAnswerList());
            map.put("试题类型", yxTestQuestionRecord.getQuestionTypeId());
            map.put("试题类型名称", yxTestQuestionRecord.getQuestionTypeName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
